from django.test import TestCase, Client
from django.contrib.auth.models import User

from pypsqueak.profile.models import *
from pypsqueak.chrome.forms import *

class TestAutomaticCreationOfProfiles(TestCase):

	def setUp(self):
		self.user = User( username = "Bob" )
		self.user.save()

	def testMonkeyPatchProfileCreation(self):
		""" Make sure that profiles are automatically created if needed """
		p = self.user.get_profile()
		self.assertEquals(p.__class__.__name__, 'UserProfile')

	def tearDown(self):
		self.user.delete()



class TestLoggingIn(TestCase):

	def setUp(self):
		self.user = User(
			username = 'Bob',
			last_name='last',
			first_name='first'
			)
		self.user.set_password( 'password' )
		self.user.save()

	def testUserSuccessLogin(self):
		""" Ensure a newly created user can log in. """
		c = Client()
		result = c.login(username=self.user.username, password='password')
		self.assertEquals( result, True)


	def testUserFailedLogin(self):
		""" Ensure a wrong password doesn't work.. """
		c = Client()
		result = c.login(username=self.user.username, password="WRONG")
		self.assertEquals( result, False )


	def tearDown(self):
		self.user.delete()


class TestProfileForms(TestCase):
	""" Make sure that the forms/views for users have correct permissions """

	def setUp(self):

		# Create test users.
		self.owner_user = User(
			username = 'staff',
			last_name='last_name',
			first_name='first_name',
			is_staff=True
			)
		self.owner_user.set_password( 'password' )
		self.owner_user.save()

		self.other_user = User(
			username = 'other',
			last_name='last_name',
			first_name='first_name',
			)
		self.other_user.set_password( 'password' )
		self.other_user.save()



	def testGroup_AnonUserFailure(self):
		""" Test Anonymous user group rights. """

		c = Client()
		self.view(c,False)


	def testGroup_OtherUserFailure(self):
		""" Test non-owner user group rights. """

		c = Client()
		response = c.login(username=self.other_user.username, password='password')
		self.assertEquals( response, True)
		self.view(c,False)

	def testGroup_OwnerSuccess(self):
		""" Test owner user's rights. """
		c = Client()
		response = c.login(username=self.owner_user.username, password='password')
		self.assertEquals( response, True)

		self.view(c,True)


	def view(self, client, result):
		# Expected results

		if result:
			equal = self.assertEqual
			contains = self.assertContains
		else:
			equal = self.assertNotEqual
			contains = self.assertNotContains

		# Does the user's home page have 'edit' link?
		response = client.get(self.owner_user.get_absolute_url())
		contains( response, self.owner_user.get_profile().get_update_url())

		# Test profile detail view to ensure edit link is/isn't shown.
		response = client.get(self.owner_user.get_profile().get_absolute_url() )
		contains(response, self.owner_user.get_profile().get_update_url())

		# Test update view throwing http error or redirect
		response = client.get( self.owner_user.get_profile().get_update_url() )
		equal( response.status_code, 200 )
		if response.status_code == 200:
			contains(response, ChromeModelForm.update )

